Skip to content

2sat输出方案(n^2) ​

cpp
bool mark[N];
int stk[N],top;
int rev(int u){
    if(u>n)return u-n;
    return u+n;
}
bool dfs(int u){
    if(mark[rev(u)])return false;
    if(mark[u])return true;
    mark[u]=true;
    stk[++top]=u;
    for(int i=h[u];~i;i=ne[i]){
        int v=e[i];
        if(!dfs(v))return false;
    }
    return true;
}
void solve(){
    for(int i=1;i<=n;i++){
        if(!mark[i]&&!mark[rev(i)]){
            top=0;
            if(!dfs(rev(i))){
                while(top>0)mark[stk[top--]]=0;
                if(!dfs(i))return false;
            }
        }
    }
}